package com.akeil.flash.dialog;

import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.PlatformUI;

public class LimisInfoOpenDialog extends TitleAreaDialog {
	DecimalFormat df4 = new DecimalFormat("0.0000");
	DecimalFormat df1 = new DecimalFormat("0.0");
	private Text textSampleNo;
	private Text textFilename;
	private Text textUsername;
	private Text textPassword;
	private Combo comboMethod;
	private Text textNote;
	
	private String sampleNo;
	private double sampleDense;
	private double sampleVolume;
	private String method;
	private int methodno;
	private String oldFilename;
	private String newFilename;
	private String username;
	private String password;
	private String note = "";
	private boolean hasMethod = false;
	
	public LimisInfoOpenDialog(Shell parentShell,String defaultFilename) {
		super(parentShell);
		// TODO Auto-generated constructor stub
		this.oldFilename = defaultFilename;
	}

	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite) super.createDialogArea(parent);
		GridLayout gridLayout = (GridLayout) composite.getLayout();
		gridLayout.horizontalSpacing = 10;
		
		Group group = new Group(composite, SWT.NONE);
		group.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		group.setText("输出报告信息");
		GridLayout gl_group = new GridLayout(3, false);
		gl_group.marginRight = 10;
		gl_group.marginLeft = 10;
		group.setLayout(gl_group);
		
		Label lblNewLabel = new Label(group, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("样品编号");
		
		textSampleNo = new Text(group, SWT.BORDER);
		textSampleNo.setText(sampleNo);
		textSampleNo.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				sampleNo = textSampleNo.getText().trim();
			}
		});
		textSampleNo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(group, SWT.NONE);
		
		
		Label lblNewLabel_1 = new Label(group, SWT.NONE);
		lblNewLabel_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_1.setText("试验方法");
		
		comboMethod = new Combo(group, SWT.NONE);
		comboMethod.setItems(new String[] {"GBT 3536_ZD", "GBT 3536_Z"});
		comboMethod.select(methodno);
		method = comboMethod.getItem(comboMethod.getSelectionIndex());
		hasMethod = true;
		
		comboMethod.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				method = comboMethod.getItem(comboMethod.getSelectionIndex());
				methodno = comboMethod.getSelectionIndex();
				hasMethod = true;
				checkParameter();
			}
		});
		
		comboMethod.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(group, SWT.NONE);		
		Label lblNewLabel_2 = new Label(group, SWT.NONE);
		lblNewLabel_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel_2.setText("文件名称");
		
		textFilename = new Text(group, SWT.BORDER);
		textFilename.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				newFilename = textFilename.getText().trim();
			}
		});
		textFilename.setText(oldFilename);
		textFilename.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Button button = new Button(group, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				Shell parentShell = PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow().getShell();
				FileDialog dialog = new FileDialog(parentShell, SWT.SAVE);
				dialog.setFilterPath("");// 设置默认的路径
				dialog.setText("生成一个XML文件");// 设置对话框的标题
				dialog.setFileName(oldFilename);// 设置默认的文件名
				dialog.setFilterNames(new String[] { "Limis文件 (*.xml)", "所有文件(*.*)" });// 设置扩展名
				dialog.setFilterExtensions(new String[] { "*.xml", "*.*" });// 设置文件扩展名
				String fileQualifiedName = dialog.open();//
								
				if(fileQualifiedName != null){
					textFilename.setText(fileQualifiedName);
					newFilename = fileQualifiedName;
					checkParameter();
				}
			}
		});
		button.setText("浏览...");
		
		Label lblLims = new Label(group, SWT.NONE);
		lblLims.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblLims.setText("Lims用户");
		
		textUsername = new Text(group, SWT.BORDER);
		textUsername.setText(username);
		textUsername.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(group, SWT.NONE);
		textUsername.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				username = textUsername.getText().trim();
			}
		});
		
		Label label = new Label(group, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("密      码");
		
		textPassword = new Text(group, SWT.BORDER | SWT.PASSWORD);
		textPassword.setText(password);
		textPassword.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(group, SWT.NONE);
		textPassword.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				password = textPassword.getText().trim();
			}
		});
		
		Label label_2 = new Label(group, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_2.setText("测试备注");
		
		textNote = new Text(group, SWT.BORDER);
		textNote.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				note = textNote.getText().trim();
			}
		});
		textNote.setText(note);
		textNote.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		new Label(group, SWT.NONE);
		
		
		this.setTitle("填写Lims参数");
		this.setMessage("请正确输入Lims参数值",IMessageProvider.INFORMATION);
		
		return composite;
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText("Lims参数");
	}
	
	@Override
	public void setErrorMessage(String newErrorMessage) {
		// TODO Auto-generated method stub
		Button okButton = getButton(IDialogConstants.OK_ID);
        if (okButton != null) 
            okButton.setEnabled(newErrorMessage == null);
        
		super.setErrorMessage(newErrorMessage);
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		// TODO 自动生成的方法存根
		super.createButtonsForButtonBar(parent);
		checkParameter();
	}
	
	public String getSampleNo() {
		return sampleNo;
	}

	public void setSampleNo(String sampleNo) {
		this.sampleNo = sampleNo;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getOldFilename() {
		return oldFilename;
	}

	public void setOldFilename(String oldFilename) {
		this.oldFilename = oldFilename;
	}

	public String getNewFilename() {
		return newFilename;
	}

	public void setNewFilename(String newFilename) {
		this.newFilename = newFilename;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getMethodno() {
		return methodno;
	}

	public void setMethodno(int methodno) {
		this.methodno = methodno;
	}

	public double getSampleDense() {
		return sampleDense;
	}

	public void setSampleDense(double sampleDense) {
		this.sampleDense = sampleDense;
	}

	public double getSampleVolume() {
		return sampleVolume;
	}

	public void setSampleVolume(double sampleVolume) {
		this.sampleVolume = sampleVolume;
	}
	
	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	private void checkParameter(){
		Pattern pattern = Pattern.compile("/^[0-9]+([.]{1}[0-9]+){0,1}$/");
		Button button = LimisInfoOpenDialog.this.getButton(IDialogConstants.OK_ID);
		if(button != null){			
			
			if(!hasMethod){
				button.setEnabled(false);
				LimisInfoOpenDialog.this.setErrorMessage("请选择方法！");
				return;
			}else{
				button.setEnabled(true);
				LimisInfoOpenDialog.this.setErrorMessage(null);
			}
			
			String fn = textFilename.getText();
			if(fn == null || fn.equals("")){
				button.setEnabled(false);
				LimisInfoOpenDialog.this.setErrorMessage("输入文件名称！");
				return;
			}else{
				button.setEnabled(true);
				LimisInfoOpenDialog.this.setErrorMessage(null);
			}
		}
	}
	
}

/**
 * 测试Gitee for new master
 * */
